var Promise = require('bluebird');
var ObjectID = require('mongodb').ObjectID;

var mongoBase = require('../common/mongo-base');
var page = require('../common/page');


module.exports = {
  userList: userList,
  showUser: showUser,
  saveUser: saveUser,
  updateUser: updateUser,
  removeUser: removeUser
};


function userList(req, res, next) {

  var query = {};
  var fields = {_id:1, userName:1, email:1, phone:1, typeName:1};
  var options = {sort:{date:-1}, limit:50};

  mongoBase.findData('user-data', query, fields, options)

    .then(function(data) {
      var resData = {
        userList: data
      }

      res.render('admin/user-list', resData);
    })

    .catch(function(err){
      console.log(err);
    });

}


function showUser(req, res, next) {
  var userId = req.query.userId;
  var typeName = req.session.user.typeName;

  if(!userId) {
    res.render('admin/user-edit', {title: '新增用户', typeName: typeName});
    return;
  }

  var query = {_id: new ObjectID(userId)};
  var fields = {_id:1, userName:1, email:1, phone:1, typeName:1, password:1};

  mongoBase.findOneData('user-data', query, fields)

    .then(function(data) {
      var resData = {
        title: '编辑用户信息',
        user: data,
        typeName: typeName
      }
      res.render('admin/user-edit', resData);
    })

    .catch(function(err){
      console.log(err);
    });
}


function saveUser(req, res, next) {

  var userName = req.body.userName;
  var email = req.body.email;
  var phone = req.body.phone;
  var typeName = req.body.typeName;
  var password = req.body.password;

  var userData = {
    userName: userName,
    email: email,
    phone: phone,
    typeName: typeName,
    password: password,
    date: new Date()
  };

  var query = {email: email};
  var fields = {_id:1, email:1};

  mongoBase.findOneData('user-data', query, fields)

    .then(function(data) {
      if(data && data.email == email) {
        return 'isExit';
      } else {
        return mongoBase.saveData('user-data', userData)
      }
    })
    
    .then(function(data) {
      if(data === 'isExit') {
        res.send(data);
        return;
      }
      userList(req, res, next);

    })

    .catch(function(err) {
      console.log(err);
    });

}


function updateUser(req, res, next) {
  var userId = req.body.userId;
  var userName = req.body.userName;
  var phone = req.body.phone;
  var typeName = req.body.typeName;
  var password = req.body.password;

  if(!userId) {
    next();
    return ;
  }

  var userData = {
    userName: userName,
    phone: phone,
    typeName: typeName,
  };

  if(password) {
    userData.password = password;
  }


  var query = {_id: new ObjectID(userId)};
  mongoBase.updateData('user-data', query, userData)
    
    .then(function(data) {

      if(userId == req.session.user._id) {
        req.session.user = null;
        res.send('login');
      } else {
        userList(req, res, next);
      }


    })

    .catch(function(err) {
      console.log(err);
    });

}


function removeUser(req, res, next) {
  var userId = req.query.userId;

  var query = {_id: new ObjectID(userId)};

  mongoBase.removeData('user-data', query)

    .then(function() {

      userList(req, res, next);

    })

    .catch(function(err){
      console.log(err);
    });
}

